package com.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.object.obKeyValue;

public class GoogleSearch {
	static JSONObject json;

	public static ArrayList<obKeyValue> getValueToGoogleSearchs(String phone)
			throws IOException {

		ArrayList<obKeyValue> valueSearchs = new ArrayList<obKeyValue>();
		URL url;
		try {
			url = new URL(
					"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&safe=active&rsz=8&q="
							+ phone + "&cr=countryVN"); 

			URLConnection connection = url.openConnection();
			connection.addRequestProperty("Referer",
					"http://technotalkative.com");

			String line;
			StringBuilder builder = new StringBuilder();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
			json = new JSONObject(builder.toString());
			try {
				JSONObject responseObject = json.getJSONObject("responseData");
				JSONArray resultArray = responseObject.getJSONArray("results");

				for (int i = 0; i < resultArray.length(); i++) {
					JSONObject m_sJobj = resultArray.getJSONObject(i);

					obKeyValue ob = new obKeyValue(m_sJobj.getString("content")
							.replace("<b>", "").replace("</b>", ""),
							m_sJobj.getString("unescapedUrl"));
					valueSearchs.add(ob);
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return valueSearchs;
	}

	public static ArrayList<String> getValueToGoogleSearch(String phone)
			throws IOException {

		ArrayList<String> valueSearchs = new ArrayList<String>();
		URL url;
		try {
			url = new URL(
					"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&safe=active&rsz=8&q="
							+ phone + "&cr=countryVN"); 

			URLConnection connection = url.openConnection();
			connection.addRequestProperty("Referer",
					"http://technotalkative.com");//			
//			URLConnection connection = url.openConnection();
//			connection.addRequestProperty("Referer",
//			"1");

			String line;
			StringBuilder builder = new StringBuilder();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
			json = new JSONObject(builder.toString());
			try {
				JSONObject responseObject = json.getJSONObject("responseData");
				JSONArray resultArray = responseObject.getJSONArray("results");
				if (resultArray.length() > 0) {
					for (int i = 0; i < resultArray.length(); i++) {
						JSONObject m_sJobj = resultArray.getJSONObject(i);
						valueSearchs.add(m_sJobj.getString("content")
								.replace("<b>", "").replace("</b>", ""));
					}
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return valueSearchs;
	}
}
